package com.situ.company.score.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.project.model.ProjectModel;
import com.situ.company.score.dao.IScoreDao;
import com.situ.company.score.model.ScoreModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class ScoreDaoImpl implements IScoreDao {

	public Integer insert(ScoreModel model) {
		String sql = "insert into score(code_emp,code_pro)values(?,?)";
		return JDBCUtil.update(sql, model.getCodeEmp(), model.getCodePro());
	}

	public Integer update(ScoreModel model) {
		StringBuffer sql = new StringBuffer("update score set score=? where code_emp=? and code_pro=? ");
		List<Object> vals = Arrays.asList(model.getScore(),model.getCodeEmp(),model.getCodePro());
		return JDBCUtil.update(sql.toString(), vals);
	}
	

	public Integer delete(ScoreModel model) {
		StringBuffer sql = new StringBuffer("delete from score ");
		List<Object> vals = appendWhere(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}

	public List<ScoreModel> selectList(ScoreModel model) {
		StringBuffer sql = new StringBuffer("select *,(select name from employee e where e.code=s.code_emp) name_emp,"
	+"(select name from project p where p.code=s.code_pro) name_pro from score s");
		List<Object> vals = appendWhere(sql, model);
//		List<String> props=Arrays.asList("code","name","pass");
		Map<String, String> props = new HashMap<>();
		props.put("codeEmp", "code_emp");
		props.put("codePro", "code_pro");
		props.put("score", "score");
		props.put("nameEmp", "name_emp");
		props.put("namePro", "name_pro");
		return JDBCUtil.queryList(sql.toString(), vals, ScoreModel.class, props);
	}
	public ScoreModel selectModel(ScoreModel model) {
		StringBuffer sql = new StringBuffer("select *,(select name from employee e where e.code=s.code_emp) name_emp,"
				+"(select name from project p where p.code=s.code_pro) name_pro from score s");
		List<Object> vals = appendWhere(sql, model);
		Map<String, String> props = new HashMap<>();
		props.put("codeEmp", "code_emp");
		props.put("codePro", "code_pro");
		props.put("score", "score");
		props.put("nameEmp", "name_emp");
		props.put("namePro", "name_pro");
		return JDBCUtil.queryModel(sql.toString(), vals, ScoreModel.class, props);
	}

	public Integer selectCount(ScoreModel model) {
		StringBuffer sql=new StringBuffer("select count(1) from score");
		List<Object> vals=appendWhere(sql, model);
		return JDBCUtil.queryInt(sql.toString(), vals);
	}
	public List<Object> appendWhere(StringBuffer sql, ScoreModel model) {
		List<Object> vals = new ArrayList<>();
		sql.append(" where 1=1 ");
		String codeEmp = model.getCodeEmp();
		if (!FmtEmpty.isEmpty(codeEmp)) {
			sql.append(" and code_emp like ? ");
			vals.add(codeEmp);
		}
		String code_pro = model.getCodePro();
		if (!FmtEmpty.isEmpty(code_pro)) {
			sql.append(" and code_pro like ? ");
			vals.add(code_pro);
		}
		if(model.isPageOn()) {
			//1-0,10
			//2-10,10
			//3-20,10
			sql.append(" limit ?,?");
			vals.add(model.getRowStart());
			vals.add(model.getPageLimit());
		}
		return vals;

	}

	
}
